package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractProduct;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ExtCproductServiceImpl implements ExtCproductService {
    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ContractDao contractDao;

    @Override
    public void save(ExtCproduct extCproduct) {
        // 新增附件时需要同步货物的小计金额；
        double extcAmount = extCproduct.getCnumber() * extCproduct.getPrice();
        extCproduct.setAmount(extcAmount);
        ContractProduct product = contractProductDao.selectByPrimaryKey(extCproduct.getContractProductId());
        product.setAmount(product.getAmount() + extcAmount);
        contractProductDao.updateByPrimaryKeySelective(product);

        // 同步合同的附件数量及总金额
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        contract.setExtNum(contract.getExtNum() + 1);
        contract.setTotalAmount(contract.getTotalAmount() + extcAmount);
        contractDao.updateByPrimaryKeySelective(contract);

        extCproductDao.insertSelective(extCproduct);
    }

    @Override
    public void update(ExtCproduct extCproduct) {
        Double extcAmount_old = extCproductDao.selectByPrimaryKey(extCproduct.getId()).getAmount();
        // 修改附件时。需要同步货物的小计金额
        double extcAmount = extCproduct.getCnumber() * extCproduct.getPrice();
        extCproduct.setAmount(extcAmount);
        ContractProduct product = contractProductDao.selectByPrimaryKey(extCproduct.getContractProductId());
        product.setAmount(product.getAmount() + extcAmount - extcAmount_old);
        contractProductDao.updateByPrimaryKeySelective(product);

        // 同步合同总金额
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        contract.setTotalAmount(contract.getTotalAmount() + extcAmount - extcAmount_old);
        contractDao.updateByPrimaryKeySelective(contract);

        extCproductDao.updateByPrimaryKeySelective(extCproduct);
    }

    @Override
    public void delete(String id) {
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);

        // 删除附件需要同步货物的小计金额
        ContractProduct product = contractProductDao.selectByPrimaryKey(extCproduct.getContractProductId());
        product.setAmount(product.getAmount() - extCproduct.getAmount());
        contractProductDao.updateByPrimaryKeySelective(product);

        // 同步合同的附件数量及总金额
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        contract.setExtNum(contract.getExtNum() - 1);
        contract.setTotalAmount(contract.getTotalAmount() - extCproduct.getAmount());
        contractDao.updateByPrimaryKeySelective(contract);

        extCproductDao.deleteByPrimaryKey(id);
    }

    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExtCproductExample example, int page, int size) {
        PageHelper.startPage(page, size);
        List<ExtCproduct> list = extCproductDao.selectByExample(example);
        return new PageInfo<ExtCproduct>(list, 5);
    }
}
